/************************************************************
 * Project Name         [Thinking_In_Java]
 * File Name            [Java_IO_RandomAccessFile.java]
 * Creation Date        [07-Jul-2014]
 * 
 * Copyright© ge.y.yang@gmail.com All Rights Reserved
 * 
 * Work hard, play harder, think big and keep fit
 ************************************************************/
package pkg_09_io.api;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * java.io.RandomAccessFile
 * 
 * @author 不落的太阳(Sean Yang)
 * @version 1.0
 * @since JDK 6
 * 
 */
public class Java_IO_RandomAccessFile {

	/**
	 * 使用java.io.RandomAccessFile追加拷贝文件
	 * 
	 * @param fileSource
	 * @param fileDest
	 */
	public static void copyFileByRandomAccess(String fileSource, String fileDest) {
		try {
			RandomAccessFile in = new RandomAccessFile(fileSource, "r");
			RandomAccessFile out = new RandomAccessFile(fileDest, "rw");

			byte[] buffer = new byte[(int) in.length()];
			in.read(buffer);
			out.write(buffer);

			in.close();
			out.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 测试方法
	 * 
	 * @param args
	 *            命令行参数
	 */
	public static void main(String[] args) {
		Java_IO_RandomAccessFile
				.copyFileByRandomAccess("test.txt", "test2.txt");
	}
}
